package com.google.gwt.it2tool.client.widget;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import com.google.gwt.it2tool.client.ClientDataCache.DataChangeHandler;
import com.google.gwt.it2tool.client.Factory;
import com.google.gwt.it2tool.client.Production;

public class SummaryFlexTable extends ProductFlexTable {
	
	private Factory factory;	
	
	public SummaryFlexTable(){
		super();
		
		setDataChangeHandler(dataChangeHandler);
		
		init();		
	}
	
	public SummaryFlexTable(Factory f){
		super();
		
		factory=f;
		init();	
	}
	
	private void init(){
		setDataChangeHandler(dataChangeHandler);
		
		setText(0, 0, "Produkt");
		setText(0, 1, "Gesamte Produktion");
		getRowFormatter().addStyleName(0, "factoryListHeader");
		getCellFormatter().addStyleName(0, 0, "summaryListProductHeader");
		getCellFormatter().addStyleName(0, 1, "summaryListNumberHeader");
		addStyleName("summaryList");
		setCellPadding(6);	
	}
	
	private void update(){
		Map<String,Integer> productionNames;
		if (factory==null){
			productionNames = getSummarizedOutputs(dataCache.getFactories().values().iterator(), dataCache.getMassProdLevel());
		}else{
			ArrayList<Factory> list = new ArrayList<Factory>();
			list.add(factory);
			productionNames = getSummarizedOutputs(list.iterator(), dataCache.getMassProdLevel());
		}
		
		int nrOfRows = getRowCount();
		for (int i=0;i<nrOfRows-1;i++){
			removeRow(getRowCount()-1);
		}
		
		String[] names = new String[productionNames.size()];
		productionNames.keySet().toArray(names);
		int size = names.length;
		for(int i=0;i<size;i++){
			setWidget(i+1, 0, getProductLabel(new Production(names[i])));
			setText(i+1, 1, productionNames.get(names[i]).toString());
			getCellFormatter().addStyleName(i+1,1,"productionOutput");
			getCellFormatter().addStyleName(i+1,1,"factoryListNumericColumn");
		}
	}
	
	private Map<String,Integer> getSummarizedOutputs(Iterator<Factory> factories, int massProdLvl){
		TreeMap<String,Integer> productionNames = new TreeMap<String,Integer>();		
		
		Production curr;
		while (factories.hasNext()){
			Iterator<Production> iterProds = factories.next().getProductions().iterator();
			while (iterProds.hasNext()){
				curr=iterProds.next();
				if (!productionNames.containsKey(curr.getName())){
					productionNames.put(curr.getName(), curr.getOutput());
				}else{
					int old = productionNames.get(curr.getName());
					productionNames.put(curr.getName(),old + curr.getOutput());
				}
			}
		}
		return productionNames;
	}
	
	private DataChangeHandler dataChangeHandler = new DataChangeHandler() {
		
		@Override
		public void onRecipeDataChanged() {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onPrototypeDataChanged() {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onMassProductionDataChanged() {
			update();			
		}
		
		@Override
		public void onFactoryDataChanged() {	
			update();
		}
	};

}
